var houseID = parseInt(GetUrlString("id"));


// 新房详情
~function getList(houseID){
  var token = localStorage.getItem("hpWrite_token");
  $.ajax({
      type:'GET',
      url:address+'/businessBuildings/get.do?format=true',
      data:{
        "id":houseID,
        "hpWrite_token":token
      },
      dataType:'json',
      success:function(result){
        if(result.status ==200){
          var basedata = result.data.businessBuildings;
          $("#slide").html(detailSlideTPL(basedata));
          doDetailSlide();
          $("#baseInfo").html(baseInfoTPL(basedata));
          $("#discountDetails").html(basedata.discountDetails);
          $("#traffic").html(basedata.traffic);
          $("#address").html(basedata.address);
          $("#call").html(callTPL(basedata));
          detailMap(basedata);
        }
      }
  })
}(houseID);


//轮播图
function detailSlideTPL(data){
  var html ='';
  html += '<ul class="slideBox">';
  $.each(data.businissImageList,function(index,info){
    html += '<li><img src="'+info+'&size=640x300"></li>';
  })
    html += '</ul>'+
    '<div class="tips p-lr10 white l-h24 abs b0 l0 w100p bg-white">'+
      '<span class="name">'+data.name+'</span>'+
      '<span class="item white abs r10 t0"><span class="current">1</span><span>&frasl;</span><span class="all">1</span></span>'+
    '</div>';
  return html;
}

//轮播图do
function doDetailSlide(){
  var slength = $('#slide').find('li').length,
      $item = $('#slide').find('.item'),
      current = 1;
  $item.find('.all').text(slength);
  var slider = Swipe(document.getElementById('slide'), {
    auto: 3000,
    callback:function(index) {
      current ++;
      current = current > slength ? 1 : current;
      $item.find('.current').text(current);
    }
  });
};

//楼盘信息
function baseInfoTPL(data){
  var data = data;
  var html = '<div class="dbox houseInfo">'+
      '<ul class="flex1">'+
        '<li><label>类型：</label>'+data.typeStr+'</li>'+
        '<li><label>楼层：</label>'+data.floot+'</li>'+
      '</ul>'+
      '<ul class="flex1">'+
        '<li><label>面积：</label>'+data.buildArea+'㎡</li>'+
        '<li><label>装修：</label>'+data.decorationStr+'</li>'+
      '</ul>'+
  '</div>'+
  '<div class="p5 m-b5 b-b">'+
    '<p><span class="gray">适合经营：</span>';
  var operateArr = data.operate.split("，");
  $.each(operateArr,function(index,info){
    if(index ==0){
      html += info ;
    }else{
      html += '，'+info ;
    }
    
  })
  html += '</p>'+
  '</div>'+
  '<div class="dbox houseInfo">'+
    '<ul class="flex1">'+
      '<li><label>楼盘名：</label>'+data.name+'</li>'+
    '</ul>'+
    '<ul class="flex1">'+
      '<li><label>区域：</label>'+data.district+'</li>'+
    '</ul>'+
  '</div>'+
  '<div class="p5">'+
      '<p><span class="gray">物业公司：</span>'+data.company+'</p>'+
    '</div>'+
    '<div class="detailLabel orange">';
    $.each(data.labelList,function(index,info){
      html += '<span>'+info+'</span>';
    })
    html += '</div>';
  return html;
}

// 百度地图
function detailMap(data){
  var map = new BMap.Map("detailMap");
  var point = new BMap.Point(data.longitude,data.latitude);
  map.centerAndZoom(point,16);
  var marker = new BMap.Marker(point);  // 创建标注
  map.addOverlay(marker);              // 将标注添加到地图中
  var label = new BMap.Label(data.name,{offset:new BMap.Size(20,-10)});
  marker.setLabel(label);
};


//房价走势图
houseChart(houseID);
$("#trend").on("click",".titleTab span",function(){
    var type = $(this).attr("data-type");
    $(this).addClass("checked").siblings().removeClass("checked");
    houseChart(houseID,type);
});
function houseChart(houseID,type){
    var type = type ? type : 1;
    $.ajax({
      type:'GET',
      url:address+'/averagePrice/stat.do',
      data:{
        "type":type,
        "newBuildingsId":houseID,
      },
      dataType:'json',
      success:function(result){
        if(result.status ==200){
          var basedata = result.data.list,ym=[],price=[];
          $.each(basedata,function(index,info){
            ym.push(info.ym);
            price.push(info.averagePrice);
          })
          chart(ym,price);
        }
      }
  })
};
// 折线图
function chart(ym,price){
  var data = {
  labels :ym,
  datasets : [
    {
        strokeColor : "rgba(243,129,46,1)",
        pointColor : "rgba(243,129,46,1)",
        pointStrokeColor : "#fff",
        data :price
      }
    ]
  }
  option ={
    pointDotRadius:2,datasetStrokeWidth:1,datasetFill:false
  }
  var ctx = document.getElementById("houseChart").getContext("2d");
  new Chart(ctx).Line(data,option);
};

function callTPL(data){
  var html = '<p>售楼电话</p>'+
  '<a href="tel:'+data.phone+'">'+data.phone+'</a>'+
  '<a href="tel:'+data.phone+'" class="callBtn"></a>';
  return html;
}

$("#discountMore").on("click",function(){
  var $this = $(this);
  if($this.hasClass("turnDown")){
    $this.removeClass("turnDown");
    $("#discountDetails").addClass("trafficH");
  }else{
    $this.addClass("turnDown");
    $("#discountDetails").removeClass("trafficH");
  }
});

$("#trafficMore").on("click",function(){
  var $this = $(this);
  if($this.hasClass("turnDown")){
    $this.removeClass("turnDown");
    $("#traffic").addClass("trafficH");
  }else{
    $this.addClass("turnDown");
    $("#traffic").removeClass("trafficH");
  }
});

